package jp.co.hcs.attendancemanagement.dialog.logic;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import jp.co.hcs.attendancemanagement.data.dao.DivisionDAO;
import jp.co.hcs.attendancemanagement.data.entity.Division;
import jp.co.hcs.attendancemanagement.data.entity.EmployeeMaster;
import jp.co.hcs.attendancemanagement.dialog.logic.dto.DivisionDTO;
import jp.co.hcs.attendancemanagement.logic.LogicBase;

/**
 * The Class DivisionListLogic.
 */
public class DivisionListLogic extends LogicBase {

  /** The emp logic. */
  private GetEmployee empLogic;

  /** The employee. */
  private EmployeeMaster employee;

  /** The div dao. */
  private DivisionDAO divDao;

  /**
   * Gets the division list by user id.
   * 
   * @param userId the user id
   * @return the division list by user id
   */
  public List<DivisionDTO> getDivisionListByUserId(String userId) {
    try {
      employee = getEmpLogic().getEmployeeById(userId);
    } catch (SQLException e) {
    }

    List<DivisionDTO> listDivisions = new ArrayList<DivisionDTO>();
    DivisionDTO division;

    List<Division> result = null;
    try {
      result = getDivDao().getDivisionListByCompanyId(employee.getCompanyId());
    } catch (SQLException e) {
      return listDivisions;
    }

    for (Division divObj : result) {
      division = new DivisionDTO();
      division.setDivisionCode(divObj.getDivisionId());
      division.setDivisionName(divObj.getDivisionName());
      division.setSelected(divObj.getDivisionId().equals(employee.getDivisionId()));
      listDivisions.add(division);
    }
    return listDivisions;
  }

  /**
   * Gets the emp logic.
   * 
   * @return the emp logic
   */
  public GetEmployee getEmpLogic() {
    if (empLogic == null) {
      empLogic = new GetEmployeeLogic();
    }

    return empLogic;
  }

  /**
   * Sets the emp logic.
   * 
   * @param empLogic the new emp logic
   */
  public void setEmpLogic(GetEmployee empLogic) {
    this.empLogic = empLogic;
  }

  /**
   * Gets the div dao.
   * 
   * @return the div dao
   */
  public DivisionDAO getDivDao() {
    if (divDao == null) {
      divDao = daoFactory.getDivisionDAO();
    }

    return divDao;
  }

  /**
   * Sets the div dao.
   * 
   * @param divDao the new div dao
   */
  public void setDivDao(DivisionDAO divDao) {
    this.divDao = divDao;
  }

}
